package com.uenpay.im.ui.old.activity.phonebook;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.qyx.android.message.type.PacketType;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.contacts.ContactsHandle;
import com.uenpay.im.callback.OnCommonCallBack;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.weight.RoundImageView;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-2-22 下午3:02:55
 * @copyRight http://www.qiyunxin.com
 * @function 手机联系人适配器
 */
public class PhoneBookAdapter extends BaseAdapter {

	private PhoneBookHandle mBookHandle = null;
	private ArrayList<MobileContatcsEntity> mArrayList = null;
	private LayoutInflater mLayoutInflater = null;

	public PhoneBookAdapter(Context _Context,
			ArrayList<MobileContatcsEntity> _ArrayList) {
		mLayoutInflater = LayoutInflater.from(_Context);
		mArrayList = _ArrayList;
		mBookHandle = new PhoneBookHandle();
	}

	@Override
	public int getCount() {
		return mArrayList == null ? 0 : mArrayList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return mArrayList == null ? null : mArrayList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		PhoneBookViewHodler mBookViewHodler = null;
		final MobileContatcsEntity mMobileContatcsEntity = mArrayList.get(arg0);
		if (arg1 == null) {
			arg1 = mLayoutInflater.inflate(
					R.layout.item_phone_book_list_layout, arg2,false);
			mBookViewHodler = new PhoneBookViewHodler();
			mBookViewHodler.nameTv = (TextView) arg1.findViewById(R.id.name_tv);
			mBookViewHodler.phoneNumTv = (TextView) arg1
					.findViewById(R.id.phone_num_tv);
			mBookViewHodler.avatarIv = (RoundImageView) arg1
					.findViewById(R.id.avatar_iv);
			mBookViewHodler.submitBtn = (Button) arg1
					.findViewById(R.id.submit_btn);
			arg1.setTag(mBookViewHodler);
		} else {
			mBookViewHodler = (PhoneBookViewHodler) arg1.getTag();
		}

		if (!TextUtils.isEmpty(mMobileContatcsEntity.name)) {
			if (TextUtils.isEmpty(mMobileContatcsEntity.nickName)) {
				mBookViewHodler.nameTv.setText(mMobileContatcsEntity.name);
			} else {
				mBookViewHodler.nameTv.setText(mMobileContatcsEntity.name + "("
						+ mMobileContatcsEntity.nickName + ")");
			}
		}

		mBookViewHodler.phoneNumTv.setText(mMobileContatcsEntity.mobilePhone);
		mBookViewHodler.avatarIv.SetUrl(APIConfiguration.getAvatarUrlNormal(
				mMobileContatcsEntity.custId, PacketType.PERSONAL));

		if (mMobileContatcsEntity._isfriend == 1) {
			mBookViewHodler.submitBtn.setVisibility(View.GONE);
		} else {
			mBookViewHodler.submitBtn.setVisibility(View.VISIBLE);
			if (mMobileContatcsEntity._isfriend == 0
					&& !TextUtils.isEmpty(mMobileContatcsEntity.custId)) {
				mBookViewHodler.submitBtn.setText(R.string.add_friends);
				mBookViewHodler.submitBtn
						.setBackgroundResource(R.drawable.button_bg_normal);
				mBookViewHodler.submitBtn.setEnabled(true);
			} else {
				if (mMobileContatcsEntity._invited == 1) {
					mBookViewHodler.submitBtn
							.setText(R.string.inviteed_contacts);
					mBookViewHodler.submitBtn
							.setBackgroundResource(R.drawable.button_bg_gray);
					mBookViewHodler.submitBtn.setEnabled(false);
				} else {
					mBookViewHodler.submitBtn.setText(R.string.invite_contacts);
					mBookViewHodler.submitBtn
							.setBackgroundResource(R.drawable.button_bg_normal);
					mBookViewHodler.submitBtn.setEnabled(true);
				}
			}
		}

		mBookViewHodler.submitBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (!TextUtils.isEmpty(mMobileContatcsEntity.mobilePhone)) {
					if (mMobileContatcsEntity._invited == 0
							&& mMobileContatcsEntity._isfriend == 0
							&& TextUtils.isEmpty(mMobileContatcsEntity.custId)) {
						String mobile = mMobileContatcsEntity.mobilePhone
								.replace(" ", "");
						invitationContacts(mobile);
					} else if (!TextUtils.isEmpty(mMobileContatcsEntity.custId)
							&& mMobileContatcsEntity._isfriend == 0) {
						addFriend(mMobileContatcsEntity.custId);
					}
				}
			}
		});
		return arg1;
	}

	private class PhoneBookViewHodler {
		private RoundImageView avatarIv;
		private TextView nameTv, phoneNumTv;
		private Button submitBtn;
	}

	/**
	 * 邀请
	 * @param username
	 */
	private void invitationContacts(final String username) {
		mBookHandle.invitationContacts(username,
				new PhoneBookHandle.IInvitationResultListener() {

					@Override
					public void onResultListener(int status, String msg) {
						if (status == 0) {
							updateContactsData(username);
						} else if (!TextUtils.isEmpty(msg)) {
							IMApplication.showToast(msg);
						}
					}
				});
	}

	private void updateContactsData(String mobilePhone) {
		for (int i = 0, size = mArrayList.size(); i < size; i++) {
			if (!TextUtils.isEmpty(mArrayList.get(i).mobilePhone)) {
				String str = mArrayList.get(i).mobilePhone.replace(" ", "");
				if (mobilePhone.equals(str)) {
					mArrayList.get(i)._invited = 1;
					notifyDataSetChanged();
					break;
				}
			}

		}

		mBookHandle.saveFriends(mArrayList);
	}

	/**
	 * 添加好友
	 * @param custId
	 */
	private void addFriend(String custId) {
		ContactsHandle.applyAddFriend(custId, "", new OnCommonCallBack() {

			@Override
			public void onSuccess(Object result) {
				IMApplication.showToast(R.string.success);
			}

			@Override
			public void onFailed(String reason) {
				if (!TextUtils.isEmpty(reason)) {
					IMApplication.showToast(reason);
				}
			}
		});
	}

}
